package com.example.qzzm;

import java.io.File;

import com.example.jx.FileUtils;
import com.example.jx.PictureUtils;
import com.example.qzzm.R;
import com.example.view.MImageView;
import com.example.view.MScrollView;
import com.example.view.MScrollView.OnScrollChangerListener;
import com.example.view.ReboundScrollView;
import com.example.view.ReboundScrollView.ReBoundScrollListener;

import android.util.FloatMath;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

/**
 * 详情页面的Activity
 * 
 * @author Administrator
 *
 */
public class CenterActivity extends Activity implements OnClickListener {
	// 返回按钮
	private ImageView ivBack;

	// 头像
	private ImageView ivUserHead;

	// 用户名
	private TextView ivUserName;

	// 背景窗口
	private MImageView ivBg;

	// scrollview
	private ReboundScrollView mScrollView;

	// 背景窗口使用的Bitmap
	private Bitmap bitmap;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post);
		bitmap = PictureUtils.drawableToBitmap(getResources().getDrawable(
				R.drawable.app_default));

		initView();
		initListeners();
	}

	// 初始化布局控件
	private void initView() {
		ivBack = (ImageView) findViewById(R.id.iv_back);
		ivUserHead = (ImageView) findViewById(R.id.iv_head_img);
		ivUserName = (TextView) findViewById(R.id.tv_user_name);

		ivBg = (MImageView) findViewById(R.id.iv_bg);

		mScrollView = (ReboundScrollView) findViewById(R.id.msc_sc1);
	}

	// 初始化监听事件
	private void initListeners() {
		ivBack.setOnClickListener(this);
		ivUserHead.setOnClickListener(this);

		mScrollView.setReBoundScrollListener(new ReBoundScrollListener() {

			@Override
			public void scrollchange(int move) {
				ivBg.setZoom(move);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_head_img:
			// 头像上传
			AlertDialog.Builder builder = new AlertDialog.Builder(
					CenterActivity.this);
			builder.setTitle("设置头像").setItems(new String[] { "本地上传", "拍照" },
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case 0:
								// 本地图片上传
								uploadImageFromLocal();
								break;
							case 1:
								// 拍照
								takePhotoNow();
								break;
							default:
								break;
							}
						}
					});
			builder.create().show();
			break;
		case R.id.iv_back:
			finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 从本地相册中上传图片
	 */
	private void uploadImageFromLocal() {
		Intent intent1 = new Intent(Intent.ACTION_PICK, null);
		intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent1, 1);

		// 返回内容：Uri对象
	}

	/**
	 * 现拍照上传
	 */
	private void takePhotoNow() {
		Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
				Environment.getExternalStorageDirectory(), "head.jpg")));
		startActivityForResult(intent2, 2);

		// 返回内容：无、图片保存在head.jpg
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			// 操作失败
			return;
		}
		switch (requestCode) {
		case 1:
			cropPhoto(data.getData());// 裁剪图片
			break;
		case 2:
			File temp = new File(Environment.getExternalStorageDirectory()
					+ "/head.jpg");
			cropPhoto(Uri.fromFile(temp));// 裁剪图片
			break;
		case 3:
			getImageToView(data);
			break;
		default:
			break;
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	private void cropPhoto(Uri fromFile) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(fromFile, "image/*");
		// 设置裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 120);
		intent.putExtra("outputY", 120);
		intent.putExtra("return-data", true);
		// 头像保存到本地
		if (null != FileUtils.getApplicationFile("user_img/", 1)) {// 创建目录
			File tempFile = FileUtils.getApplicationFile(
					"user_img/user_head.jpg", 0);
			intent.putExtra("output", Uri.fromFile(tempFile)); // 传入目标文件
		}
		startActivityForResult(intent, 3);
	}

	/**
	 * 保存裁剪之后的图片数据
	 * 
	 * @param picdata
	 */
	private void getImageToView(Intent data) {
		if (data != null) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(photo);
				ivUserHead.setImageDrawable(drawable);
				// 然后上传图片到服务器

			}
		} else {
			ivUserHead
					.setImageResource(R.drawable.umeng_socialize_default_avatar);
		}
	}
}
